<?php
$db = null;

function db_open()
{
	global $cfg;
	global $db;
	
	if ($db == null)
	{
		$db = mysql_connect($cfg["db_hostname"], $cfg["db_user"], $cfg["db_pass"], true);
		if ($db)
		{
			if (!mysql_select_db($cfg["db_name"], $db))
			{
				die("Cannot select database (" . mysql_error() . ")");
				mysql_close($db);
				$db = null;
			}
		}
		else
		{
			die("Cannot connect to database (" . mysql_error() . ")");
		}
	}
	
	return $db != null;
}

function db_close()
{
	global $db;

	if ($db != null)
	{
		mysql_close($db);
		$db = null;
	}
}
?>